import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-09-04
 * Time: 16:33
 */
public class MyStack {
    private int[] elem;

    private int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }
    //判断栈满不满
    public boolean isFull() {
        return elem.length == usedSize;
    }

    // 压栈
    public void push(int val) {
        if(isFull()) {
            this.elem = Arrays.copyOf(elem, elem.length*2);
        }
        this.elem[usedSize] = val;
        this.usedSize++;
    }
    //出栈
    public int pop() {
        if(empty()) {
            throw new StackEmptyExceptiom("此时栈为空!");
        }
        return elem[--usedSize];
    }
    //判断栈空不空
    public boolean empty() {
        return this.usedSize == 0;
    }
    //返回栈顶元素
    public int peek() {
        if(empty()) {
            throw new StackEmptyExceptiom("此时栈为空!");
        }
        return elem[usedSize-1];
    }

}
